//2.- 从键盘输入几本书的数据，每本书的数据包括书名,价格，出版社，
// 将每项数据分别写入文本文件 s.txt和二进制文件s.dat.
#include <stdio.h>
#include <stdlib.h>

typedef struct bookinfo
{
    char name[100];
    double price;
    char publish[100];
} Book;

int main(void)
{
    Book book[4];
    int i;
    FILE *fp1,*fp2;
    fp1 = fopen("bookinfo.txt","w");
    fp2 = fopen("bookinfo.dat","w");

    if(fp1 == NULL)
    {
        fprintf(stderr,"bookinfo.txt open failed!");
        exit(1);
    }
    
    if(fp2 == NULL)
    {
        fprintf(stderr,"bookinfo.dat open failed!");
        exit(1);
    }

    printf("Enter 4 book info like(C language 50.0, Pear)\n");
    for(int i = 0; i < 4; i++)
    {
        scanf("%s%lf%s",book[i].name,&book[i].price,book[i].publish);
    }

    for(i = 0; i < 4; i++)
    {
       printf("%s %.2f %s\n",book[i].name,book[i].price,book[i].publish);
       fprintf(fp1,"%s %.2lf %s\n",book[i].name,book[i].price,book[i].publish);
       fprintf(fp2,"%s %.2lf %s\n",book[i].name,book[i].price,book[i].publish);
    }
    fclose(fp1);
    fclose(fp2);
    exit(0);
}
